package gui;

import gui.listeners.KeyShortcutsListener;
import gui.panels.jtree.database.DataBaseCategoryInfo;
import gui.panels.jtree.database.DataBaseCategoryNode;
import gui.panels.jtree.database.DataBaseDndTree;
import gui.panels.jtree.filesystem.FileSystemDndTree;
import gui.panels.media.MediaCapturePanel;
import gui.panels.media.MediaContainer;
import gui.panels.media.MediaPanel;
import gui.playbook.PlayBook;
import gui.settings.SettingsEditor;
import gui.statistics.MainStatisticsPanel;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Event;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.media.Player;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

import conf.PropReaderWriter;
import java.awt.Dimension;
import java.util.logging.Level;

import javax.swing.JTextField;
import javax.swing.JButton;

import logger.ApplicationLogger;
import java.awt.GridBagLayout;
import javax.swing.JSlider;
import java.awt.GridBagConstraints;
import java.awt.Font;
import javax.swing.border.SoftBevelBorder;
import java.awt.Color;

public class MainWindow {

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="14,10"
	private JTabbedPane tabPanel = null;
	private JPanel pnlVideo = null;
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu editMenu = null;
	private JMenu helpMenu = null;
	private JMenuItem exitMenuItem = null;
	private JMenuItem aboutMenuItem = null;
	private JMenuItem saveMenuItem = null;
	private JDialog aboutDialog = null;
	private JPanel aboutContentPane = null;
	private JLabel aboutVersionLabel = null;
	private JTree hierachyCategoryTree = null;
	private JPanel mediaPanel = null;
	private JMenuItem endMenuItem;  //  @jve:decl-index=0:
	private JScrollPane scrPnlHierachy = null;
	private JPanel pnlMediaCapture = null;
	private JPanel pnlStatistics = null;
	private JMenuItem editSettingsMenuItem;
	private JPanel pnlPlayBook;
	private DefaultMutableTreeNode root;
	private JPanel pnlTimeLine = null;
	private JSlider sldTimeLine = null;
	private JLabel lblZoomTimeLine = null;
	private JPanel pnlTimeLineRep = null;
	private JLabel lblTmlRep = null;
	private JPanel pnlTmlControls = null;
	private JButton btnCutControlTml = null;
	private JLabel lblControlsTime = null;
	
	public MainWindow(){
		initialize();
	}
	
	private void initialize(){
		this.getJFrame().setVisible(true);
		this.updateSettings();
		
	}

	private JPanel getPnlStatistics(){
		if(pnlStatistics==null){
			pnlStatistics = new MainStatisticsPanel();
		}

		return pnlStatistics;
	}

	private JTabbedPane getTabPanel(){
		if(tabPanel==null){
			tabPanel = new JTabbedPane();

			tabPanel.setSize(1026, 682);
			tabPanel.setTabPlacement(JTabbedPane.LEFT);
			String nomVideo = PropReaderWriter.getLangProperty("video");
			String nomStat = PropReaderWriter.getLangProperty("statistics");
			String nomPlayBook = PropReaderWriter.getLangProperty("play_book");
			tabPanel.addTab(nomVideo, getPnlVideo());
			tabPanel.addTab(nomStat, getPnlStatistics());
			tabPanel.addTab(nomPlayBook, getPnlPlayBook());

		}
		return tabPanel;
	}
	private JPanel getPnlPlayBook() {
		if(pnlPlayBook==null){
			pnlPlayBook = new JPanel();
			pnlPlayBook.setLayout(null);
			PlayBook gamePanel = new PlayBook();
			gamePanel.setBounds(new Rectangle(2, 61, 730, 505));
			pnlPlayBook.add(gamePanel);
		}
		return pnlPlayBook;
	}

	/**
	 * This method initializes jFrame
	 * 
	 * @return javax.swing.JFrame
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setJMenuBar(getJJMenuBar());
			jFrame.setSize(1009, 742);
			jFrame.setContentPane(getJContentPane());
			jFrame.setTitle("Application");
			jFrame.setExtendedState(jFrame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
			//jFrame.addKeyListener(new KeyShortcutsListener());

		}
		return jFrame;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getTabPanel());
		}
		return jContentPane;
	}

	private JPanel getPnlVideo() {
		if (pnlVideo == null) {
			pnlVideo = new JPanel();
			pnlVideo.setLayout(null);
			pnlVideo.add(getMediaPanel());
			pnlVideo.setSize(900,500);
			pnlVideo.add(getPnlHierachyCategoryTree(), null);
			pnlVideo.add(getPnlMediaCapture(), null);
			pnlVideo.add(getPnlTimeLine(), null);
		}
		return pnlVideo;
	}


	private JScrollPane getPnlHierachyCategoryTree() {
		if(scrPnlHierachy==null){
			scrPnlHierachy = new JScrollPane(getHierachyCategoryTree());
			scrPnlHierachy.setBounds(new Rectangle(3, 3, 178, 668));
			scrPnlHierachy.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));

		}
		return scrPnlHierachy;
	}
	private JPanel getMediaPanel() {
		if(mediaPanel == null){
			mediaPanel = new MediaPanel();
			mediaPanel.setBounds(new Rectangle(186, 1, 354, 263));
			mediaPanel.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
			


		}
		return mediaPanel;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getEditMenu());
			jJMenuBar.add(getHelpMenu());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getSaveMenuItem());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getEditMenu() {
		if (editMenu == null) {
			editMenu = new JMenu();
			
			String nomEdit = PropReaderWriter.getLangProperty("edit");
			editMenu.setText(nomEdit);
			
			
			editMenu.add(getEditSettingsMenuItem());

		}
		return editMenu;
	}

	private JMenuItem getEditSettingsMenuItem() {
		if(editSettingsMenuItem==null){
			editSettingsMenuItem = new JMenuItem();
			String nomConf = PropReaderWriter.getLangProperty("configuration");
			editSettingsMenuItem.setText(nomConf);
			editSettingsMenuItem.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					SettingsEditor se = new SettingsEditor();
					se.addActionListener(new SettingsListener(getMainWindow()));
					se.setVisible(true);

				}


			});
		}
		return editSettingsMenuItem;
	}

	private MainWindow getMainWindow(){
		return this;
	}
	private JMenuItem getToggleEndItem() {
		if (endMenuItem == null) {
			endMenuItem = new JMenuItem();
			String nomTE = PropReaderWriter.getLangProperty("toggle_end");
			
			endMenuItem.setText(nomTE);
			endMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Player player = ((MediaContainer)getMediaPanel()).getPlayer();
					((MediaContainer)getMediaPanel()).setStopTime(player.getMediaNanoseconds());

					//TODO Loguear el inicio y el fin
					System.out.println("FIN marcado: "+player.getMediaNanoseconds());
				}
			});
			endMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
					Event.CTRL_MASK, true));
		}
		return endMenuItem;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(getAboutMenuItem());
		}
		return helpMenu;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("About");
			aboutMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialog aboutDialog = getAboutDialog();
					aboutDialog.pack();
					Point loc = getJFrame().getLocation();
					loc.translate(20, 20);
					aboutDialog.setLocation(loc);
					aboutDialog.setVisible(true);
				}
			});
		}
		return aboutMenuItem;
	}

	/**
	 * This method initializes aboutDialog	
	 * 	
	 * @return javax.swing.JDialog
	 */
	private JDialog getAboutDialog() {
		if (aboutDialog == null) {
			aboutDialog = new JDialog(getJFrame(), true);
			aboutDialog.setTitle("About");
			aboutDialog.setContentPane(getAboutContentPane());
		}
		return aboutDialog;
	}

	/**
	 * This method initializes aboutContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getAboutContentPane() {
		if (aboutContentPane == null) {
			aboutContentPane = new JPanel();
			aboutContentPane.setLayout(new BorderLayout());
			aboutContentPane.add(getAboutVersionLabel(), BorderLayout.CENTER);
		}
		return aboutContentPane;
	}

	/**
	 * This method initializes aboutVersionLabel	
	 * 	
	 * @return javax.swing.JLabel	
	 */
	private JLabel getAboutVersionLabel() {
		if (aboutVersionLabel == null) {
			aboutVersionLabel = new JLabel();
			aboutVersionLabel.setText("Version 1.0");
			aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return aboutVersionLabel;
	}



	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getSaveMenuItem() {
		if (saveMenuItem == null) {
			saveMenuItem = new JMenuItem();
			saveMenuItem.setText("Save");
			saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
					Event.CTRL_MASK, true));
		}
		return saveMenuItem;
	}

	/**
	 * This method initializes hierachyCategoryTree	
	 * 	
	 * @return javax.swing.JTree	
	 */
	private JTree getHierachyCategoryTree() {
		if(hierachyCategoryTree==null){
			
			hierachyCategoryTree= new JTree();
			hierachyCategoryTree.setSize(200,200);
			String dataStoreType = PropReaderWriter.getProperty("data_store_type");
			String dataUrl = PropReaderWriter.getProperty("data_url");
			if(dataStoreType.equals("1")){
				DataBaseCategoryInfo dbc = new DataBaseCategoryInfo();
				root = new DataBaseCategoryNode(dbc);
				hierachyCategoryTree = new DataBaseDndTree(root);
				hierachyCategoryTree.setSize(200,200);
			}else if(dataStoreType.equals("2")){
				root = FileSystemDndTree.addNodes(null, dataUrl);
				hierachyCategoryTree = new FileSystemDndTree(root);
				hierachyCategoryTree.setSize(200,200);

			}else{
				hierachyCategoryTree = new JTree();
				hierachyCategoryTree.setSize(200,200);
			}
			
		}

		return hierachyCategoryTree;


	}

	/**
	 * This method initializes pnlMediaCapture	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlMediaCapture() {
		if (pnlMediaCapture == null) {
			pnlMediaCapture = new MediaCapturePanel();
			pnlMediaCapture.setBounds(new Rectangle(543, 2, 361, 262));
			pnlMediaCapture.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
		}
		return pnlMediaCapture;
	}
	/**
	 * This method initializes pnlTimeLine	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlTimeLine() {
		if (pnlTimeLine == null) {
			lblTmlRep = new JLabel();
			lblTmlRep.setBounds(new Rectangle(5, 81, 69, 26));
			lblTmlRep.setText("Timeline");
			lblZoomTimeLine = new JLabel();
			lblZoomTimeLine.setBounds(new Rectangle(4, 39, 67, 25));
			lblZoomTimeLine.setText("Zoom");
			pnlTimeLine = new JPanel();
			pnlTimeLine.setLayout(null);
			pnlTimeLine.setBounds(new Rectangle(183, 268, 719, 290));
			pnlTimeLine.add(getSldTimeLine(), null);
			pnlTimeLine.add(lblZoomTimeLine, null);
			pnlTimeLine.add(getPnlTimeLineRep(), null);
			pnlTimeLine.add(lblTmlRep, null);
			pnlTimeLine.add(getPnlTmlControls(), null);
		}
		return pnlTimeLine;
	}

	/**
	 * This method initializes sldTimeLine	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getSldTimeLine() {
		if (sldTimeLine == null) {
			sldTimeLine = new JSlider();
			sldTimeLine.setBounds(new Rectangle(75, 43, 416, 16));
		}
		return sldTimeLine;
	}

	/**
	 * This method initializes pnlTimeLineRep	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlTimeLineRep() {
		if (pnlTimeLineRep == null) {
			pnlTimeLineRep = new JPanel();
			pnlTimeLineRep.setLayout(new GridBagLayout());
			pnlTimeLineRep.setBounds(new Rectangle(80, 88, 401, 8));
			pnlTimeLineRep.setBorder(BorderFactory.createLineBorder(Color.gray, 5));
		}
		return pnlTimeLineRep;
	}

	/**
	 * This method initializes pnlTmlControls	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlTmlControls() {
		if (pnlTmlControls == null) {
			lblControlsTime = new JLabel();
			lblControlsTime.setBounds(new Rectangle(45, 8, 100, 16));
			lblControlsTime.setFont(new Font("Dialog", Font.BOLD, 12));
			lblControlsTime.setHorizontalTextPosition(SwingConstants.CENTER);
			lblControlsTime.setHorizontalAlignment(SwingConstants.CENTER);
			lblControlsTime.setText("Controles");
			pnlTmlControls = new JPanel();
			pnlTmlControls.setLayout(null);
			pnlTmlControls.setBounds(new Rectangle(524, 17, 188, 256));
			pnlTmlControls.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			pnlTmlControls.add(getBtnCutControlTml(), null);
			pnlTmlControls.add(lblControlsTime, null);
		}
		return pnlTmlControls;
	}

	/**
	 * This method initializes btnCutControlTml	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCutControlTml() {
		if (btnCutControlTml == null) {
			btnCutControlTml = new JButton();
			btnCutControlTml.setBounds(new Rectangle(15, 34, 34, 10));
		}
		return btnCutControlTml;
	}

	/**
	 * Launches this application
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainWindow application = new MainWindow();
				
			}
		});
	}
	
	public void updateSettings(){
		ApplicationLogger.getLogger().log(Level.INFO, "Updating settings in main window");
		KeyboardFocusManager kman = KeyboardFocusManager.getCurrentKeyboardFocusManager();
		kman.addKeyEventDispatcher( new KeyShortcutsListener((MediaContainer) this.getMediaPanel()));
		String dataStoreType = PropReaderWriter.getProperty("data_store_type");
		String dataUrl = PropReaderWriter.getProperty("data_url");
				
		DefaultTreeModel tm = (DefaultTreeModel) hierachyCategoryTree.getModel();
		if(dataStoreType.equals("1")){
			DataBaseCategoryInfo dbc = new DataBaseCategoryInfo();
			root = new DataBaseCategoryNode(dbc);
			
		}else if(dataStoreType.equals("2")){
			root = FileSystemDndTree.addNodes(null, dataUrl);
		}else{
			hierachyCategoryTree = new JTree();
			hierachyCategoryTree.setSize(200,200);
		}
		tm.setRoot(root);
		tm.reload( root);
		
		hierachyCategoryTree.revalidate();
		hierachyCategoryTree.repaint();
	}

}

class SettingsListener implements ActionListener{
	
	private MainWindow mainWindow;
	
	public SettingsListener(MainWindow mainWindow){
		this.mainWindow = mainWindow;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		mainWindow.updateSettings();
		
	}
	
}
